package com.itrip.itripbiz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrip.itripbiz.service.HotelService;
import com.itrip.itripbiz.util.BaseQuery;
import com.itrip.itripbiz.util.EmptyUtils;
import com.itrip.itripbiz.util.Page;
import com.itrip.itripbiz.util.PropertiesUtils;
import com.itrip.itripbiz.vo.SearchHotelVO;
import com.itrip.itripbiz.vo.itripHotelVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 酒店表 服务实现类
 * </p>
 *
 * @author 四阶段项目二组
 * @since 2020-02-25
 */
@Service
public class HotelServiceImpl  implements HotelService {
    private HttpSolrClient httpSolrClient;
    @Autowired
    private HotelService hotelService;

    public static String URL = PropertiesUtils.get("application.properties", "spring.data.solr.host");

    private BaseQuery<itripHotelVo> itripHotelVOBaseQuery = new BaseQuery(URL);


    @Override
    public Page<itripHotelVo> searchItripHotelPage(SearchHotelVO hotelVO, Integer pageNo, Integer pageSize) throws Exception {
        SolrQuery query=new SolrQuery("*:*");
        StringBuffer tempQuery=new StringBuffer();
        int tempFlag=0;

        if (EmptyUtils.isNotEmpty( hotelVO)) {
            //按照目的地搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getDestination())) {
                tempQuery.append("destination:"+hotelVO.getDestination()+"*");
                tempFlag=1;
            }
            //按照酒店星级搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getHotelLevel())) {
                query.addFilterQuery("hotelLevel:"+hotelVO.getHotelLevel());
            }
            //按照关键字进行搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getKeywords())) {
                if (tempFlag==1) {
                    query.addFilterQuery("keywords:"+hotelVO.getKeywords());
                   /* tempQuery.append("and keywords:"+hotelVO.getKeywords());*/

                }else {
                    query.addFilterQuery("keywords:"+hotelVO.getKeywords());
                   /* tempQuery.append(" keywords:"+hotelVO.getKeywords());*/
                }
            }
            //按照酒店特色搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getFeatureId())) {
                StringBuffer buffer=new StringBuffer("(");
                int flag=0;
                String featureIdArray[]= hotelVO.getFeatureId().split(",");
                for (String featureId:featureIdArray) {
                    if (flag==0) {
                        buffer.append("featureIds:"+"*,"+featureId+",");
                    }else {
                        buffer.append("OR featureIds:"+"*,"+featureId+",");
                    }
                    flag++;

                }
                buffer.append(")");
            }
            //按照酒店商圈搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getTradinfAreaId())) {
                StringBuffer buffer = new StringBuffer("(");
                int flag = 0;
                String tradeAreaIdArray[] = hotelVO.getTradinfAreaId().split(",");
                for (String tradeAreaId : tradeAreaIdArray) {
                    if (flag == 0) {
                        buffer.append("OR tradinfAreaIds:" + "*," + tradeAreaId + ",*");
                    } else {
                        buffer.append("  tradinfAreaIds:" + "*," + tradeAreaId + ",*");
                    }
                    flag++;
                }
                buffer.append(")");
                query.addFilterQuery(buffer.toString());
            }
            //是数字就是true
        /*    if (!StringUtils.isNumeric(hotelVO.getMaxPrice())) {
                hotelVO.setMaxPrice("");
            }*/
           /* if (!StringUtils.isNumeric(hotelVO.getMinPrice())) {
                hotelVO.setMinPrice("");
            }*/
            //按照酒店价格区间搜索
            if (EmptyUtils.isNotEmpty(hotelVO.getMaxPrice())) {
                query.addFilterQuery("minPrice:" + "[* TO " + hotelVO.getMaxPrice() + "]");
            }
            if (EmptyUtils.isNotEmpty(hotelVO.getMinPrice())) {
                query.addFilterQuery("minPrice:" + "[" + hotelVO.getMinPrice() + " TO *]");
            }
            //升序 排序
            if (EmptyUtils.isNotEmpty(hotelVO.getAscSort())) {
                query.addSort(hotelVO.getAscSort(), SolrQuery.ORDER.asc);
            }
            //降序 排序
            if (EmptyUtils.isNotEmpty(hotelVO.getDescSort())) {
                query.addSort(hotelVO.getDescSort(), SolrQuery.ORDER.desc);
            }
        }

        if (EmptyUtils.isNotEmpty(tempQuery.toString())) {
            query.setQuery(tempQuery.toString());
        }
        Page<itripHotelVo> page = itripHotelVOBaseQuery.queryPage(query, pageNo, pageSize, itripHotelVo.class);

        return page;
    }

    @Override
    public List<itripHotelVo> searchItripHotelListByHotCity(Integer cityId, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        if (EmptyUtils.isNotEmpty(cityId)) {
            query.addFilterQuery("cityId:" + cityId);
        } else {
            return null;
        }
        List<itripHotelVo> hotelVOList = itripHotelVOBaseQuery.queryList(query, pageSize, itripHotelVo.class);
        return hotelVOList;
    }
}
